function oauth_init() {
	//Hit server, passing ina signed request and opensocial ID to see if we know who it is
	osapi.cache.invalidate();
	osapi.http.get({
		'href' : 'https://boruapps.com/_gadget/index.php',
		'format' : 'json',
		'authz' : 'signed',
		'noCache' : true
	}).execute(handleLoadResponse);
}

function handleLoadResponse(data) {
	c=data.content;
//	console.log(c)
	if (!c.user_exists) {
		//User doesn't exist, need to do IpenID to match user ID to OpenID
		//Rerieve the domain of the current user. gadgets.util.getUrlParameters()['parent'] returns a value
		// of of the form: http(s)://mail.google.com/mail/domain.com/html for Gmail (other containers are similar).
		// The example below shows a regular expression for use with Gmail. For Calendar, use this regular
		// expression instead: /calendar\/hosted\/([^\/]+)/

		var token = data.content.token; 
		var text = document.createTextNode('Sign in');

		var url_root="https://boruapps.com/_gadget/popup.php";
		var domain = gadgets.util.getUrlParameters()['parent'].match(/.+\/a\/(.+)\/html/) [1]; 
		var url = url_root + '?domain=' + domain + '&token=' + token;
		var func = 'openPopup("' + url + '")';

		make_button(text,func)
	  } else {
		//User exists, OpenID must have occurred previously
		save_pref(c);
		account_tab();
		//do gadget

	  }
}

function openPopup(url) {
  var popup = window.open(url, 'OpenID','height=250,width=200');

  // Check every 100 ms if the popup is closed.
  finishedInterval = setInterval(function() {
	// If the popup is closed, we've either finished OpenID, or the user closed it. Verify with the server in case the
	// user closed the popup.
	if (popup.closed) {
	  osapi.http.get({
		'href' : 'https://boruapps.com/_gadget/index.php',
		'format' : 'json',
		'authz' : 'signed',
		'noCache' : true
	  }).execute(handleLoadResponse);

	  clearInterval(finishedInterval);
	}
  }, 100);
}

function delete_user() {
	  osapi.http.get({
		'href' : 'https://boruapps.com/_gadget/delete.php',
		'format' : 'json',
		'authz' : 'signed',
		'noCache' : true 
	  }).execute(handleDeleteResponse);
}

function handleDeleteResponse(data) {
	c=data.content;
	clearButton();
	var text = document.createTextNode(c.status);
	document.getElementById("oauthstatus").appendChild(text);
	var prefs = new gadgets.Prefs();
	prefs.set("vtigerurl", '');
	prefs.set("username", '');
	prefs.set("accesskey", '');
}
	

function save_pref(c) {
	var prefs = new gadgets.Prefs();
	prefs.set("vtigerurl", c.vtdomain);
	prefs.set("username", c.vtuser);
	prefs.set("accesskey", c.vtaccesskey);
}

function make_button(text,func) {

	var button = document.createElement('a');
	button.setAttribute('href', 'javascript:void(0);');
	button.setAttribute('onclick', func);

	button.appendChild(text);
	clearButton();
	document.getElementById("oauthstatus").appendChild(button);
}

function clearButton() {
	var element = document.getElementById("oauthstatus");
	while (element.firstChild) {
		element.removeChild(element.firstChild);
	}
}


function oauth_main() {
	var prefs = new gadgets.Prefs();
	while (prefs.getString("vtigerurl")=='') {
		init();
	}
	var vurl = prefs.getString("vtigerurl");
	MHM.challenge();
}
